/*
 * Copyright(C) 2010 Luvina Software Company
 *
 * LoginManaged.java, Oct 29, 2010 ntmhuong
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;

import javax.faces.event.PhaseEvent;

import net.luvina.manageuser.managedbean.LoginSession;
import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.dto.LoginDto;
import net.luvina.manageuser.logic.LoginLogic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * LoginUserManaged
 *
 * @author ntmhuong
 *
 */
@Component("loginManaged")
@Scope("request")
public class LoginManaged extends UserBaseManaged implements Serializable {
	private static final long serialVersionUID = -2197417007714072153L;
	private String loginId;
	private String password;
	private String errMessage;

	@Autowired
	private LoginLogic loginLogic;
	@Autowired
	protected LoginSession loginSession;

	/**
	 * doInit
	 *
	 * @param event
	 *            The PhaseEvent object
	 */
	public void doInit(PhaseEvent event) {

	}

	/**
	 * doLogin
	 *
	 * @return screen list
	 */
	public String doLogin() {
		LoginDto loginDto = this.loginLogic.doLogin(this.loginId, this.password);
		// login success
		if (loginDto.isLoginFlg()) {
			this.loginSession.createLoginInfo(loginDto);

			// login unsuccess
		} else {
			this.errMessage = "";
			errMessage = loginDto.getErrMsg();
			return CommonConstant.LOGIN;
		}
		return CommonConstant.ADM002;
	}

	/**
	 * doLogout
	 *
	 * @return screen login
	 */
	public String doLogout() {
		this.loginSession.clear();
		return CommonConstant.LOGIN;
	}

	/**
	 * @return the loginId
	 */
	public String getLoginId() {
		return loginId;
	}

	/**
	 * @param loginId
	 *            the loginId to set
	 */
	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the errMessage
	 */
	public String getErrMessage() {
		return errMessage;
	}

	/**
	 * @param errMessage
	 *            the errMessage to set
	 */
	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}
}
